package com.liwbn.rbac.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liwbn.rbac.dao.RolePermissionMapper;
import com.liwbn.rbac.model.RolePermission;
import com.liwbn.rbac.model.RolePermissionExample;
import com.liwbn.rbac.model.RolePermissionExample.Criteria;
import com.liwbn.rbac.service.RolePermissionService;

@Service
public class RolePermissionServiceImp implements RolePermissionService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public Long insert(RolePermission model) {
        return rolePermissionMapper.insert(model);
    }

    @Override
    public int insertSelective(RolePermission model) {
        return rolePermissionMapper.insertSelective(model);
    }

    @Override
    public int updateByPrimaryKey(RolePermission model) {
        return rolePermissionMapper.updateByPrimaryKey(model);
    }

    @Override
    public int updateByPrimaryKeySelective(RolePermission model) {
        return rolePermissionMapper.updateByPrimaryKeySelective(model);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return rolePermissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public RolePermission selectByPrimaryKey(Long id) {
        return rolePermissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updatePermissionByRole(Map<String, String> permiss) {
        Long id;
        String value;
        RolePermission rp;
        String[] vs;
        List<Long> ls = new ArrayList<Long>();
        List<RolePermission> rps = new ArrayList<RolePermission>();
        RolePermissionExample example = new RolePermissionExample();
        for (Map.Entry<String, String> entry : permiss.entrySet()) {
            example.clear();
            Criteria criteria = example.createCriteria();
            ls.clear();
            rps.clear();
            id = Long.parseLong(entry.getKey());
            criteria.andRoleIdEqualTo(id);
            rolePermissionMapper.deleteByExample(example);
            value = entry.getValue();
            if (StringUtils.isNotBlank(value)) {
                vs = value.split(",");
                for (String s : vs) {
                    if (StringUtils.isNotBlank(s)) {
                        ls.add(Long.parseLong(s));
                    }
                }
                if (ls.size() > 0) {
                    // 批量插入角色权限信息
                    for (Long l : ls) {
                        rp = new RolePermission();
                        rp.setRoleId(id);
                        rp.setPermissionId(l);
                        rps.add(rp);
                    }
                    if (rps.size() > 0) {
                        rolePermissionMapper.insertBatch(rps);
                    }
                }
            }
        }
    }

}
